package cn.itsource.org.controller;

import cn.itsource.basic.utli.AjaxResult;
import cn.itsource.basic.utli.PageList;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    //查询所有
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<Employee> queryAll(){
        return employeeService.querytAll();
    }

    //基于id查询
    @GetMapping("/{id}")
    @ApiOperation(value = "基于id查询")
    //@PathVariable 获取将地址栏上面的id
    public Employee queryById(@PathVariable("id") Long id){
        return employeeService.queryByid(id);
    }

    //分页查询
    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query){
        System.out.println(query);
        return  employeeService.queryByPage(query);
    }

    //新增或者修改
    @PutMapping
    @ApiOperation(value = "新增或者修改")
    public AjaxResult saveOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                 employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    //删除
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxResult delete(@PathVariable("id") long id){
        try {
            employeeService.delect(id);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    //批量删除
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            employeeService.batchDeletedelect(ids);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
}















